package com.tim.helper;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ApolloHelper {

    @Test
    public void apps(){
        String url = "";
        String Cookie = "";

        HttpRequest request = HttpUtil.createRequest(Method.GET, url)
                .header("Cookie", Cookie);
        String respBody = request.execute().body();
        System.out.println(JSONUtil.formatJsonStr(respBody));
    }

    @Test
    public void appConfigVals(){
        String appInfos = "";
        {
            appInfos = "";
        }
        List<String> fields = Arrays.asList("");
        String urlTemplate = "http://10.168.160.69:9090/configs/%s/default/application";

        StringBuilder builder = new StringBuilder();
        for(String app : appInfos.trim().split("\\s+")){
            String url = String.format(urlTemplate, app);
            String respBody = HttpUtil.createRequest(Method.GET, url)
                    .execute().body();
            JSONObject jsonObject = JSON.parseObject(respBody);
            JSONObject configurations = jsonObject.getJSONObject("configurations");
            for(String field : fields){
                String val = configurations!=null&&configurations.containsKey(field)?configurations.getString(field):"NONE";
                builder.append(val).append("\t");
            }
            builder.append("\n");
        }
        System.out.println(builder.toString());


    }


}
